%matplotlib inline
import os
host = os.environ['CASHOST']
port = os.environ['CASPORT']
userid = None
password = None
# needed to start a CAS server.
from swat import *
sess = CAS(host, port,userid, password)
from dlpy.images import ImageTable
my_images = ImageTable.load_files(sess, path='/dept/cas/leliuz/Data/Demo/Giraffe_Dolphin')
my_images = ImageTable.from_table(my_images)
my_images.head()
sess.tableinfo()
my_images.columninfo()
# randomly select 8 images from the table and arrange the display in 4 columns
my_images.show(nimages=8,ncol=4, randomize=True)
my_images.label_freq
my_images.image_summary
Note: each image processing function has two mode, specified by Inplace:
We will show this in the resize function, and all other functions work similarly.
resized_images = my_images.resize(width=128, height=128, inplace=False)
resized_images.show(8,4)
resized_images.image_summary
my_images.show(8,4)
my_images.image_summary
Note: the processed images are in the resized_images table, and the original table is not modified.
my_images.resize(width=224, height=224, inplace=True)
my_images.show(8,4)
my_images.image_summary
Note: the original table has been modified.
cropped_images = my_images.crop(x=50, y=50, width=100, height=100, inplace=False)
x, y specify the coordinate of the top-left corner of the cropped images.
width, height specify the size of the cropped images.
cropped_images.show(8,4)
cropped_images.image_summary
aug_images = my_images.as_patches(x=0, y=0, width=112, height=112, step_size=56,
output_width=112, output_height=112, inplace=False)
x, y specify the coordinate of the top-left corner of the first crop.
width, height specify the size of the crop windows.
step_size specify the step size of the moving windows for extracting the crops.
output_width, output_height specify the size of the output images.
aug_images.show(15,3, randomize=False)
my_images.label_freq
aug_images.label_freq
aug_images.image_summary
Note: the function enrichs the data set in the table by create multiple crops from each of the images in the table.
raug_images = my_images.as_random_patches(random_ratio=0.5, x=0, y=0, width=112, height=112, step_size=56,
output_width=112, output_height=112, inplace=False)
random_ratio (between 0 and 1) specifies the proportion of the generated crops to write into the output table.
raug_images.show(15,3, randomize=False)
my_images.label_freq
raug_images.label_freq
raug_images.image_summary
Note that the only difference between as_patches and as_random_patches is that the former one keep all the crops while the later one keep part of them.
Save the images in the table to a specified path on the server. For security purpose, if you want to save images locally, please save them on server side and use ftp or scp to copy or move them to your local directory.
my_images.to_files(path = '/dept/cas/leliuz/Data/Demo/New_folder')
Note: path must be an existing directory on the server. The format must meet the server OS requirements.
https://developer.sas.com/apis/swat/python/v1.1.0/api.html#serialization-io-conversion
sess.endsession()